﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestionePolveriLib.Facades;
using GestionePolveriLib.BL;

namespace GestionePolveri2
{
    public enum PolvereLottoEditorViews
    {
        ByPolvere = 0,
        ByLotto
    }

    public partial class PolvereLottoEditor : UserControl
    {
        PolvereLottoEditorViews m_polvereLottoEditorView = PolvereLottoEditorViews.ByPolvere;
        Polvere m_polvere = null;

        public PolvereLottoEditorViews PolvereLottoEditorView
        {
            get { return m_polvereLottoEditorView; }
            set 
            { 
                m_polvereLottoEditorView = value;

                InitializeControl();

                //if (m_polvereLottoEditorView == PolvereLottoEditorViews.ByPolvere)
                //{
                //    InitializeControlByPolvere();
                //}
                //else
                //{
                //    InitializeControlByLotto();
                //}
            }
        }
        
        public Polvere Polvere
        {
            get { return m_polvere; }
            set 
            { 
                m_polvere = value;

                this.cmbCMB1.Text = m_polvere.CodicePolvere;
            }
        }
 
        public Lotto Lotto { get; set; }

        public PolvereLottoEditor()
        {
            InitializeComponent();

            if (!this.DesignMode)
            {
                
            }
        }

        private void InitializeControl()
        {
            if (m_polvereLottoEditorView == PolvereLottoEditorViews.ByPolvere)
            {
                lblCMB1.Text = "Polveri:";
                lblCMB2.Text = "Lotti:";



                InitializeByPolvere();
            }
            else
            {
                lblCMB1.Text = "Lotti:";
                lblCMB2.Text = "Polveri:";

                this.cmbCMB1.Location = new Point(7, 66);
                this.lookUpEdit1.Location = new Point(7, 22);

                InitializeByLotti();
            }
        }

        private void InitializeByLotti()
        {
            InitializeControlByLotto();
        }

        private void InitializeByPolvere()
        {
            InitializeControlByPolvere();
        }

        void lookUpEdit1_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e)
        {
            if (e.CloseMode == DevExpress.XtraEditors.PopupCloseMode.Normal)
            {
                if (e.Value != null)
                {
                    Lotto = (Lotto)e.Value;

                    if (m_polvereLottoEditorView == PolvereLottoEditorViews.ByPolvere)
                    {
                    }
                    else
                    {
                        this.cmbCMB1.DataSource = null;
                        this.cmbCMB1.Items.Clear();

                        if (Lotto != null)
                        {
                            PolvereFacade pf = new PolvereFacade();
                            List<Polvere> m_polveri = new List<Polvere>();

                            m_polveri = pf.GetbyLotto(Lotto.IDLotto);
                            if (m_polveri != null)
                            {
                                m_polveri.Insert(0, new Polvere() { CodicePolvere = "Seleziona una Polvere", ID = -1 });

                                this.cmbCMB1.DisplayMember = "CodicePolvere";
                                this.cmbCMB1.ValueMember = "ID";
                                this.cmbCMB1.DataSource = m_polveri;

                                this.cmbCMB1.SelectedIndexChanged += new EventHandler(cmbPolveri2_SelectedIndexChanged);
                            }
                        }
                    }
                }
            }
        }











        #region SELEZIONE PER LOTTI
        private void InitializeControlByLotto()
        {
            this.lblCMB1.Text = "Lotti:";
            this.lblCMB2.Text = "Polveri:";

            LottoFacade lf = new LottoFacade();
            List<Lotto> m_lotti = new List<Lotto>();

            m_lotti = lf.GetLotti();
            if (m_lotti != null)
            {
                lookUpEdit1.Properties.DisplayMember = "CodLotto";
                //lookUpEdit1.Properties.ValueMember = "CodLotto";
                lookUpEdit1.Properties.DataSource = m_lotti;
                lookUpEdit1.CloseUp += new DevExpress.XtraEditors.Controls.CloseUpEventHandler(lookUpEdit1_CloseUp);
                //lookUpEdit1.EditValue = "1";
            }
        }

        void cmbLottiPolveri_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Lotto = (Lotto)this.cmbCMB1.SelectedItem;

            //this.cmbCMB2.DataSource = null;
            //this.cmbCMB2.Items.Clear();

            //if (Lotto != null)
            //{
            //    PolvereFacade pf = new PolvereFacade();
            //    List<Polvere> m_polveri = new List<Polvere>();

            //    m_polveri = pf.GetbyLotto(Lotto.IDLotto);
            //    if (m_polveri != null)
            //    {
            //        m_polveri.Insert(0, new Polvere() { CodicePolvere = "Seleziona una Polvere", ID = -1 });

            //        this.cmbCMB2.DisplayMember = "CodicePolvere";
            //        this.cmbCMB2.ValueMember = "ID";
            //        this.cmbCMB2.DataSource = m_polveri;

            //        this.cmbCMB2.SelectedIndexChanged += new EventHandler(cmbPolveri2_SelectedIndexChanged);
            //    }
            //}
        }

        void cmbPolveri2_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_polvere = (Polvere)cmbCMB1.SelectedItem;
        }
        #endregion

        #region SELEZIONE PER POLVERI
        private void InitializeControlByPolvere()
        {
            this.lblCMB1.Text = "Polveri:";
            this.lblCMB2.Text = "Lotti:";
            this.cmbCMB1.Location = new Point(7, 22);
            this.lookUpEdit1.Location = new Point(7, 66);

            PolvereFacade pf = new PolvereFacade();
            List<Polvere> m_polveri = new List<Polvere>();

            m_polveri.Add(new Polvere() { CodicePolvere = "Seleziona una polvere", ID = -1 });
            m_polveri.AddRange(pf.GetAll());

            this.cmbCMB1.DisplayMember = "CodicePolvere";
            this.cmbCMB1.ValueMember = "ID";
            this.cmbCMB1.DataSource = m_polveri;

            this.cmbCMB1.SelectedIndexChanged += new EventHandler(cmbPolveri_SelectedIndexChanged);

            //this.lookUpEdit1.EditValue = this.Lotto.CodLotto;
        }

        void cmbPolveri_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_polvere = (Polvere)this.cmbCMB1.SelectedItem;

            this.cmbCMB2.DataSource = null;
            this.cmbCMB2.Items.Clear();

            LottoFacade lf = new LottoFacade();
            List<Lotto> m_lotti = new List<Lotto>();

            m_lotti = lf.GetLotti(m_polvere.ID);
            if (m_lotti != null)
            {
                lookUpEdit1.Properties.NullText = "Selezionare un lotto...";

                lookUpEdit1.Properties.DisplayMember = "CodLotto";
                //lookUpEdit1.Properties.ValueMember = "CodLotto";
                lookUpEdit1.Properties.DataSource = m_lotti;
                lookUpEdit1.CloseUp += new DevExpress.XtraEditors.Controls.CloseUpEventHandler(lookUpEdit1_CloseUp);
                //lookUpEdit1.EditValue = Lotto.CodLotto;
            }
            else
            {
                lookUpEdit1.Properties.NullText = "Nessun lotto disponibile!";
            }
        }

        //void cmbPolveriLotti_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    Lotto = (Lotto)cmbCMB2.SelectedItem;
        //}
        #endregion
    }
}
